package it.free.maya.worddistinct;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;

/**
 * Words processor to extract words from database produced list of rows
 * @author Stefano Falconetti
 */
public class WordsDistinctExtractor {

	private static Logger logger = Logger.getLogger(WordsDistinctExtractor.class); 

	private HashMap<String, String> myValues = null;
	
	/**
	 * Class builder accepting data from the database
	 */
	public WordsDistinctExtractor(HashMap<String, String> values){
		this.myValues = values;
	}
	
	/**
	 * Extract distinct words from all the rows
	 * @param rows is the list of rows to process
	 * @return a list of words extracted from the text of the list passed
	 * @throws Exception
	 */
	private HashMap<String, Word> getWordsDistinct() throws Exception{
		
		Word word = null;
		HashMap<String, Word> wordsList = null;
		
		String rowValue = null;
		String token = null;
		StringTokenizer stringTokenizer = null;
		int loopCount = 0;
		
		wordsList = new HashMap<String, Word>();
		Set<String> justRows = this.myValues.keySet();
		    
		Iterator<String>  rowsIterator = justRows.iterator();
		    
		while(rowsIterator.hasNext()){
		    	  
		      rowValue = rowsIterator.next();
		    	  
		      if(rowValue != null){
		    		 
		    	 logger.info(loopCount + " >> row value: [" + rowValue + "]");
		    		 
		    	 loopCount++;
		    		 
		    	  stringTokenizer = new StringTokenizer(rowValue);
		    		 
		    	  while(stringTokenizer.hasMoreTokens()){
		    			 
		    			token = stringTokenizer.nextToken();
		    			 
		    			if(wordsList.containsKey(token)){
		    			   wordsList.get(token).increaseCounter();
		    			   logger.info("updated token: [" + wordsList.get(token).getWord() + "]:" + wordsList.get(token).getOccurencesCounter());
		    			}else{
		    				  word = new Word();
		    				  word.setWord(token);
		    				  word.increaseCounter();
		    				  wordsList.put(token, word);
		    				  logger.info("added token: [" + word.getWord() + "]:" + word.getOccurencesCounter());
		    		   }
		          }
		      }
		}
		logger.info("Data load completed, processed " + loopCount + " database rows, " +  wordsList.size() + " words");
		
		return wordsList;
	}
	
	/**
	 * Create a sorted list of Word objects
	 * @return the sorted map
	 * @throws Exception
	 */
	public HashMap<String, Word> process() throws Exception {
		
		HashMap<String, Word> sortedWords = this.getWordsDistinct();
		
		return sortedWords;
	}
	
}
